---@class jsUtil : Object
local XBB = {}

--- 根据路径读取技能
---@param package Package
XBB.loadSkillSkelsByName = function(package, name)
  local path = "./packages/xiaobai/pkg/" .. name .. "/skills"
  local skels = {}
  local normalized_dir = path
      :gsub("^%.+/", "")
      :gsub("/+$", "")
      :gsub("/", ".")
  for _, filename in ipairs(FileIO.ls(path)) do
    -- 别加载下划线开头的模板技能哈
    if filename:sub(-4) == ".lua" and filename ~= "init.lua" and not filename:startsWith("_") then
      local skel = Pcall(require, normalized_dir .. "." .. filename:sub(1, -5))
      if skel then
        -- 如果返回了一个skel表
        if type(skel) == "table" and not skel.class then
          for _, v in ipairs(skel) do
            table.insert(skels, v)
          end
        else
          table.insert(skels, skel)
        end
      end
    end
  end
  package:loadSkillSkels(skels)
end

local changehe_generals = {} ---@type General[]

--- 小白杯新建武将
---@param package Package @ 武将所属包
---@param name string @ 武将名字
---@param kingdom string @ 武将所属势力
---@param hp integer @ 武将初始体力
---@param maxHp integer? @ 武将初始最大体力
---@param gender Gender? @ 武将性别
XBB.newGeneral = function(package, name, kingdom, hp, maxHp, gender)
  local general = General:new(package, name, kingdom, hp, maxHp, gender)
  local changhe_card = fk.CreateCard{
    name = name.."_card",
    number = 9,
    skill = "shade_skill",
    suit = Card.Spade,
    type = Card.TypeTrick,
    is_passive = true,
  }
  table.insert(changehe_generals, general)
  package:loadCardSkels{changhe_card}
  package:addCardSpec(name.."_card")
  return general
end

--- 创建所有包之后给所有【天下大白】设置翻译
XBB.addChangheCardTranslation = function ()
  for _, g in ipairs(changehe_generals) do
    Fk:loadTranslationTable{
      [g.name.."_card"] = Fk:translate(g.name),
    }
    local skills = g.other_skills
    local skill_strings = ""
    for _, s in ipairs(skills) do
      skill_strings = skill_strings .. Fk:translate(s) .. "：" .. Fk:translate(":"..s) .. "<br><br>"
    end
    Fk:loadTranslationTable{
      [":"..g.name.."_card"] = skill_strings,
    }
  end
end

--- 检查字符串是否以"xiaobai__"开头且以"_card"结尾
---@param str string @ 要检查的字符串
---@return boolean @ 如果字符串符合条件则返回true，否则返回false
XBB.startsWithEndsWith = function (str)
    local prefix = "xiaobai__"
    local suffix = "_card"
    
    -- 检查是否以prefix开头
    local starts = string.find(str, prefix, 1, true) == 1
    -- 检查是否以suffix结尾
    local ends = string.find(str, suffix, -#suffix, true) == (#str - #suffix + 1)
    
    return starts and ends
end

return XBB
